#include "AnimationManager.h"

AnimationManager::AnimationManager(void)
{
}

AnimationManager::~AnimationManager(void)
{
	std::map<std::string, Animation*>::iterator i;
	for (i = animations.begin(); i != animations.end(); i++)
		delete (*i).second;
	
	animations.clear();
}

void AnimationManager::Load(const char *path, const char *ext)
{
	std::string searchPath = path;
	searchPath += "\\*.";
	searchPath += ext;

	WIN32_FIND_DATA fd;

	HANDLE hfile = FindFirstFile(searchPath.c_str(), &fd);
	if (hfile != INVALID_HANDLE_VALUE)
	{
		BOOL doSearch = true;
		while (doSearch)
		{
			std::string animPath = path;
			animPath += "\\";
			animPath += fd.cFileName;
			Animation *anim = Animation::LoadFromFile(animPath.c_str());

			std::string animFileName = fd.cFileName;
			Add(animFileName.substr(0, animFileName.size() - 4).c_str(), anim);

			doSearch = FindNextFile(hfile, &fd);
		}

		FindClose(hfile);
	}
}

void AnimationManager::Add(const char *name, Animation *res)
{
	animations[name] = res;
}

Animation * AnimationManager::Get(const char *name)
{
	std::map<std::string, Animation*>::iterator i = animations.find(name);
	if (i == animations.end())
		return NULL;

	return (*i).second;
}

int AnimationManager::GetCount()
{
	return (int)animations.size();
}

void AnimationManager::Get(int i, char *name, Animation *&res)
{
	std::map<std::string, Animation*>::iterator it = animations.begin();
	while (i--)
		it ++;

	strcpy(name, (*it).first.c_str());
	res = (*it).second;
}
